package com.skydroid.demo.ui.activity

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.RadioGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.skydroid.demo.R
import com.skydroid.demo.helper.LiveWindowHelper
import com.skydroid.demo.utils.MEIZU
import com.skydroid.demo.utils.MIUI
import com.skydroid.demo.utils.PermissionUtils

class MainActivity : AppCompatActivity() {

    private var mContext: Context? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.mContext = this
        requestPermissions()
        initView()

    }

    private fun initView(){
        findViewById<View>(R.id.btn_data_transmission).setOnClickListener {
            DataTransmissionActivity.start(mContext!!)
        }

        var view_decode = findViewById(R.id.view_decode) as? RadioGroup
        findViewById<View>(R.id.btn_player_rtsp).setOnClickListener {

            when(view_decode?.checkedRadioButtonId){
                R.id.btn_soft->{
                    LiveWindowActivity.start(mContext!!,0)
                }
                R.id.btn_hardware->{
                    LiveWindowActivity.start(mContext!!,1)
                }
            }


        }
        findViewById<View>(R.id.btn_live_player).setOnClickListener {

            showLiveWindow()
        }

        findViewById<View>(R.id.btn_channel_check).setOnClickListener {

            ChannelCheckActivity.start(mContext!!)
        }

        findViewById<View>(R.id.btn_test).setOnClickListener {

            TestActivity.start(mContext!!)
        }



    }

    private fun requestPermissions() {
        ActivityCompat.requestPermissions(
            this, arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 0
        )
    }



    private fun showLiveWindow() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(mContext)) {
                //没有悬浮窗权限,跳转申请
                Toast.makeText(applicationContext, "请开启悬浮窗权限", Toast.LENGTH_LONG).show()
                //魅族不支持直接打开应用设置
                if (!MEIZU.isMeizuFlymeOS()) {
                    val intent = Intent(
                        Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:$packageName")
                    )
                    startActivityForResult(intent, 0)
                } else {
                    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                    startActivityForResult(intent, 0)
                }
            } else {
//                LiveUtils.initLive(this@MainActivity)
                LiveWindowHelper.initLive(mContext!!)
                finish()
            }
        } else {
            //6.0以下　只有MUI会修改权限
            if (MIUI.rom()) {
                if (PermissionUtils.hasPermission(mContext)) {
//                    LiveUtils.initLive(this@MainActivity)
                    LiveWindowHelper.initLive(mContext!!)
                    finish()
                } else {
                    MIUI.req(mContext)
                }
            } else {
//                LiveUtils.initLive(this@MainActivity)
                LiveWindowHelper.initLive(mContext!!)
                finish()
            }
        }
    }


}
